Skip to content

feat(asyncpg): Add query source to execute, executemany, and cursor methods#6242

Open
ericapisani wants to merge 14 commits into
masterfrom
py-2305-add-query-source-to-methods-that-were-missing-it
Open

feat(asyncpg): Add query source to execute, executemany, and cursor methods#6242
ericapisani wants to merge 14 commits into
masterfrom
py-2305-add-query-source-to-methods-that-were-missing-it

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented May 8, 2026

Summary

  • Add add_query_source() calls to _wrap_connection_method and _wrap_cursor_method, which cover execute(), executemany(), prepare(), _bind_exec(), and _exec. Previously only fetch() had query source tracking via the patched _wrap_execute method.
  • Handle StreamedSpan vs regular span cases separately (StreamedSpan needs the call inside the context manager).

Migrates the asyncpg integration to use `record_sql_queries_supporting_streaming` and `StreamedSpan`, adding support for the `stream` trace lifecycle. Tests are parameterized to cover both static and streaming modes.

Fixes PY-2305
Fixes #6003
…an has been created. This allows the traces_sampler to see the attributes and make decisions based on them
Add add_query_source() calls to execute(), executemany(), and cursor() methods. Previously only fetch() had query source tracking. Handle StreamedSpan and regular span cases separately since StreamedSpan requires the call while still inside the context manager.

Add comprehensive test coverage for query source tracking across all wrapped methods.
@ericapisani ericapisani requested a review from a team as a code owner May 8, 2026 17:12
@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 8, 2026

PY-2305

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

Codecov Results 📊

146 passed | Total: 146 | Pass Rate: 100% | Execution Time: 21.53s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests 📈 +73
Failed Tests
Skipped Tests 📉 -73

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 0.00%. Project has 14383 uncovered lines.
✅ Project coverage is 34.9%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
asyncpg.py 9.80% ⚠️ 138 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    29.50%    34.90%     +5.4%
==========================================
  Files          190       190         —
  Lines        22082     22094       +12
  Branches      7426      7434        +8
==========================================
+ Hits          6514      7711     +1197
- Misses       15568     14383     -1185
- Partials       662       807      +145

Generated by Codecov Action

Base automatically changed from py-2305-migrate-asyncpg to master May 11, 2026 11:53
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment re: naming in tests https://github.com/getsentry/sentry-python/pull/6242/changes#r3225427754

Otherwise lgtm!

Comment thread tests/integrations/asyncpg/test_asyncpg.py Outdated
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we add query source to _wrap_cursor_creation()?

My understanding is that the add_query_source() function is intended to add the query source to functions that execute SQL queries, which are only the following:

  • asyncpg.Connection.execute
  • asyncpg.Connection._execute
  • asyncpg.Connection._executemany

The parameter description says the following:

:param enable_db_query_source: When enabled, the source location will be added to database queries.

Can we ensure that only spans for these functions have query source added?

@ericapisani
Copy link
Copy Markdown
Member Author

@alexander-alderman-webb

Why do we add query source to _wrap_cursor_creation()?

When this PR was initially created, I hadn't yet noticed the underlying issue with how we've patched cursors, and that this only captured the creation of the CursorFactory (which included the SQL query first passed in). Spans wouldn't be created as the cursor iterated through a result set.

At this point, due to the introduction of #6252 , this PR is somewhat out of date in that cursor creation is no longer patched - the underlying BaseCursor methods are.

This means that, with those changes:

  • spans are created when _bind_exec is called, and we want to record the query source because this results in the first set of rows being retrieved
  • spans are created when _exec is called, as this method processes the next fetch

and we'll want to add query sources to those.

@ericapisani ericapisani marked this pull request as draft May 12, 2026 15:59
@ericapisani ericapisani changed the title feat(asyncpg): Add query source to execute, executemany, and cursor feat(asyncpg): Add query source to execute, executemany, and cursor methods May 12, 2026
@ericapisani ericapisani marked this pull request as ready for review May 12, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants